// pages/mine/Receive/Receive.js

const App = getApp()
// let AJAX = require("../../../utils/util.js").AJAX
// let timeTranfer = require("../../../utils/util.js").timeTranfer
let { AJAX, timeTranfer, imgUrl } = require("../../../utils/util.js")
Page({

	/**
	 * 页面的初始数据
	 */
  data: {  
    curNav: 0,
     navH:0,
    couponList: [],   
    isDisabled: false ,//是否领取
    is_nodata:false,
    imgUrl: imgUrl
  },

	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    
     this.setData({
        navH: App.globalData.navHeight
     })
     if (!wx.getStorageSync("ACCESSTOKEN")) {
        App.userInfoReadyCallback = res => {
           wx.setStorageSync('ACCESSTOKEN', res);
           this.requestList();
        }
        return
     }
     this.requestList();
  },

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {

  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  },
  jump(e){
    //console.log(e.currentTarget.id)\
    AJAX('coupon-member/receive-coupon','POST',{
      coupon_id:e.currentTarget.id
    }).then(res => {
      if (res.status =='succeed'){
        console.log("领取成功")
        //刷新当前页面的数据
        getCurrentPages()[getCurrentPages().length - 1].onLoad()

      }
    
      
    })
  },
  navBack(){
     wx.navigateBack()
  },
  navClickEvent(e) {
    this.setData({
      curNav: e.currentTarget.dataset.index
    })
  },
  requestList() {
    AJAX('coupon/list').then(res => {  
       
      let _data = res.data
       if (_data!=''){
          _data.forEach((val, i) => {
             _data[i].effective_time = timeTranfer(val.effective_time, 1)
             _data[i].floor_price = parseInt(val.floor_price)
             _data[i].price = parseInt(val.price)
          })
          this.setData({
             couponList: _data,
             is_nodata: true
          })
        
      }
     
    })
  }
})